package com.hqyj.mybatisplusdemo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.mybatisplusdemo.model.Db1;
import com.hqyj.mybatisplusdemo.service.Db1Service;
import com.zaxxer.hikari.SQLExceptionOverride;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2024-10-20
 */
@RestController
@RequestMapping("/db1")
public class Db1Controller {

    @Value("${pagesize}")
    private Integer pagesize;
    @Autowired
    private Db1Service db1Service;

    @RequestMapping("/list")
    public List<Db1> findAll(){
        List<Db1> list = db1Service.list();
        return list;
    }

    @RequestMapping("save")
    public String add(Db1 db1){
        db1Service.save(db1);
        return "success";
    }

    @RequestMapping("update")
    public String update(@RequestBody Db1 db1){
        db1Service.updateById(db1);
        return "success";
    }

    @RequestMapping("delete")
    public String delete(Integer id){
        db1Service.removeById(id);

        SQLException e;
        return "success";
    }

    @RequestMapping("/dynamicQuery1")
    public List<Db1> dynamicQuery1(String createname){
        return db1Service.selectByConditionWrapper(createname);
    }

    @RequestMapping("/dynamicQuery2")
    public List<Db1> dynamicQuery2(String createname, Integer minid, Integer maxid){
        return db1Service.selectByWrapper2(
                createname,minid,maxid
        );
    }

    @RequestMapping("/dynamicQuery3")
    public List<Db1> dynamicQuery3(String createname, Integer minid, Integer maxid){
        return db1Service.selectByWrapper3(createname,minid,maxid);
    }

    @RequestMapping("/dynamicQuery5")
    public void selectByWrapper5(String createname, int minid, int maxid) {
        QueryWrapper<Db1> queryWrapper = new QueryWrapper<Db1>();
        queryWrapper.likeRight("createname", createname).and(ws -> ws.lt("createid", maxid).or().isNotNull("updateid"));
        List<Db1> userList = db1Service.list(queryWrapper);
    }

    @RequestMapping("/page1")
    public Page<Db1> page1(Integer pageno, String create_time){
        Page<Db1> page = new Page<Db1>(pageno,pagesize);
        QueryWrapper<Db1> wrapper = new QueryWrapper<Db1>();
        wrapper.likeRight("create_time",create_time);
        return db1Service.page(page, wrapper);
    }
}

